﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace SYH.Public.DAL
{
    public class PublicAlbumImg
    {
        #region  增
        /// <summary>
        /// 添加图片，对相册图片个数已+1
        /// </summary>
        /// <param name="Model"></param>
        /// <returns></returns>
        public int Add(Model.PublicAlbumImg Model)
        {
            SqlParameter[] parameters = { 
                                                new SqlParameter("@AlbumId",SqlDbType.Int,4),
                                                new SqlParameter("@Name",SqlDbType.NVarChar,100),
                                                new SqlParameter("@UserId",SqlDbType.Int,4),
                                                new SqlParameter("@Size",SqlDbType.Int,4),
                                                new SqlParameter("@Title",SqlDbType.NVarChar,50),
                                                 new SqlParameter("@Extension",SqlDbType.NVarChar,10),
                                                  new SqlParameter("@Prefix",SqlDbType.NVarChar,50)

                                            };
            parameters[0].Value = Model.AlbumId;
            parameters[1].Value = Model.Name;
            parameters[2].Value = Model.UserId;
            parameters[3].Value = Model.Size;
            parameters[4].Value = Model.Title;
            parameters[5].Value = Model.Extension;
            parameters[6].Value = Model.Prefix;

            return Common.TypeParse.ObjectToInt(Common.SqlHelper.ExecuteScalar(Config.WebConfig.SqlConnString,
                CommandType.StoredProcedure,
                "Public_AlbumImg_Add",
                parameters), 0);
        }
        #endregion

        #region 查
        /// <summary>
        /// 得到一个对象实体 根据主键
        /// </summary>		
        public Model.PublicAlbumImg GetModel(int Id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Id, AlbumId, Name, UserId, Size, IsDelete, IsOld, Url, CreateTime  ");
            strSql.Append("  from Public_AlbumImg ");
            strSql.Append(" where Id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)};
            parameters[0].Value = Id;

            SqlDataReader sdr = Common.SqlHelper.ExecuteReader(Config.WebConfig.SqlConnString, CommandType.Text, strSql.ToString(), parameters);
            if (sdr.HasRows)
            {
                Model.PublicAlbumImg model = new Model.PublicAlbumImg();
                if (sdr.Read())
                {
                    model.Id = sdr.GetInt32(sdr.GetOrdinal("Id"));
                    model.AlbumId = sdr.GetInt32(sdr.GetOrdinal("AlbumId"));
                    model.Name = sdr.GetString(sdr.GetOrdinal("Name"));
                    model.UserId = sdr.GetInt32(sdr.GetOrdinal("UserId"));
                    model.Size = sdr.GetInt32(sdr.GetOrdinal("Size"));
                    model.IsDelete = sdr.GetBoolean(sdr.GetOrdinal("IsDelete"));
                    model.IsOld = sdr.GetBoolean(sdr.GetOrdinal("IsOld"));
                    model.Url = sdr.GetString(sdr.GetOrdinal("Url"));
                    model.CreateTime = sdr.GetDateTime(sdr.GetOrdinal("CreateTime"));
                    sdr.Close();
                    return model;
                }
            }
            sdr.Close();
            return null;
        }
        #endregion

        #region 删
        /// <summary>
        /// 删除同一相册内多张图片
        /// </summary>
        /// <param name="IdList">Id 以,号分隔</param>
        /// <param name="AlbumId">相册Id</param>
        /// <param name="Num">删除总个数</param>
        /// <returns></returns>
        public void Delete(string IdList, int AlbumId, int Num)
        {
            Common.SqlHelper.ExecuteNonQuery(Config.WebConfig.SqlConnString,
                CommandType.StoredProcedure,
                "Public_AlbumImg_Delete",
                new SqlParameter("@IdList", IdList),
                new SqlParameter("@AlbumId", AlbumId),
                new SqlParameter("@Num", Num));
        }
        #endregion

        #region 修改图片名
        public int UpdateAlbumImgName(string id, string title)
        {
            SqlParameter[] parameters = { 
                                                new SqlParameter("@Id",id),
                                                new SqlParameter("@Title",title),
                                            };
            parameters[0].Value = id;
            parameters[1].Value = title;
            return Common.TypeParse.ObjectToInt(Common.SqlHelper.ExecuteScalar(Config.WebConfig.SqlConnString, CommandType.Text, "UPDATE Public_AlbumImg SET Title=@Title WHERE Id=@Id ", parameters), 0);
        }
        #endregion

        #region 改
        /// <summary>
        /// 更新一个字段
        /// </summary>
        /// <param name="name">字段名</param>
        /// <param name="value">值</param>
        /// <param name="strWhere">条件</param>
        /// <param name="flag">字符串表示：true为字符串；false为整型、bool、日期等</param>
        /// <returns></returns>
        public bool UpdateOneField(string name, string value, string strWhere, bool flag)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("UPDATE Public_AlbumImg set ");
            strSql.Append(" " + name + "= ");
            if (flag)
            {
                strSql.Append(" '" + value + "' ");
            }
            else
            {
                strSql.Append(" " + value + " ");
            }
            strSql.Append(" WHERE " + strWhere);

            return Common.SqlHelper.ExecuteSql(Config.WebConfig.SqlConnString, strSql.ToString()) > 0;
        }
        #endregion

        #region 修改图片详细
        public int UpdateIngDetail(int id, string title, string summary)
        {
            SqlParameter[] parameters = { 
                                                new SqlParameter("@Id",id),
                                                new SqlParameter("@Title",title),
                                                new SqlParameter("@summary",summary)
                                            };
            return Common.TypeParse.ObjectToInt(Common.SqlHelper.ExecuteScalar(Config.WebConfig.SqlConnString,
                CommandType.StoredProcedure,
                "Public_AlbumImgDetail_Update",
                parameters), 0);
        }
        #endregion

        #region 根据主键ID获取某一字段
        /// <summary>
        /// 根据主键ID获取某一字段
        /// </summary>
        /// <param name="fieldName">字段</param>
        public object GetOneFieldValueById(int Id, string fieldName)
        {
            if (string.IsNullOrEmpty(fieldName))
            {
                fieldName = "0";
            }
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT " + fieldName + " FROM Public_AlbumImg  ");
            strSql.Append("WHERE Id=@Id");
            SqlParameter[] parameters = {
					new SqlParameter("@Id", SqlDbType.Int,4)};
            parameters[0].Value = Id;

            return Common.SqlHelper.ExecuteScalar(Config.WebConfig.SqlConnString, CommandType.Text, strSql.ToString(), parameters);
        }
        #endregion

        #region 根据条件获取相册列表
        /// <summary>
        /// 根据条件获取相册列表
        /// </summary>
        /// <param name="top"></param>
        /// <param name="strWhere"></param>
        /// <returns></returns>
        public IList<SYH.Public.Model.PublicAlbumImg> GetSimpleList(int top, string strWhere)
        {
            string _top = top > 0 ? string.Format("top {0}", top) : string.Empty;
            StringBuilder strSql = new StringBuilder();
            strSql.AppendFormat("SELECT {0} * ", _top);
            strSql.Append(" FROM Public_AlbumImg ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" WHERE " + strWhere);
            }
            //strSql.Append("order by" + filedOrder);
            List<SYH.Public.Model.PublicAlbumImg> list = new List<SYH.Public.Model.PublicAlbumImg>();
            SqlDataReader sdr = Common.SqlHelper.ExecuteReader(Config.WebConfig.SqlConnString, strSql.ToString());
            try
            {
                if (sdr.HasRows)
                {
                    SYH.Public.Model.PublicAlbumImg model;
                    while (sdr.Read())
                    {
                        model = new SYH.Public.Model.PublicAlbumImg();
                        model.Id = sdr.GetInt32(sdr.GetOrdinal("Id"));
                        model.Name = sdr.GetString(sdr.GetOrdinal("Name"));
                        model.CreateTime = sdr.GetDateTime(sdr.GetOrdinal("CreateTime"));
                        model.Url = sdr.GetString(sdr.GetOrdinal("Url"));
                        model.UserId = sdr.GetInt32(sdr.GetOrdinal("UserId"));
                        model.AlbumId = sdr.GetInt32(sdr.GetOrdinal("AlbumId"));
                        model.IsDelete = sdr.GetBoolean(sdr.GetOrdinal("IsDelete"));
                        model.Prefix = sdr.GetString(sdr.GetOrdinal("Prefix"));
                        model.Extension = sdr.GetString(sdr.GetOrdinal("Extension"));
                        model.Title = sdr.GetString(sdr.GetOrdinal("Title"));
                        list.Add(model);
                    }
                }
                return list;
            }
            catch (System.Exception ex)
            {
                throw new System.Exception(ex.Message);
            }
            finally
            {
                sdr.Close();
                sdr.Dispose();
            }
        }
        #endregion
    }
}
